package mbsl.books;

import mbsl.commons.lib.BasicElement;

/** An author */
public class Author extends BasicElement {
	/** Returns the full name of the author in the format Surname, Forename */
	@Override
	public String getName() {
		if (getFirstName() == null)
			return getSurname();
		else
			return getSurname() + ", " + getFirstName();
	}
	/** Sets the names of the author.
	 * <p>
	 * The argument is expected to be a name in the Surname, Forename format.  To be precise, the
	 * name will be split at the first comma (,), and the part before the comma is set as the
	 * surname, and the part after the comma (including subsequent commas) is set as the first
	 * name. If the argument has no comma, the whole text will be set as the surname.
	 */
	@Override
	public void setName(String pName) {
		int index = pName.indexOf(',');
		
		if (index < 0)
			setSurname(pName);
		else {
			setSurname(pName.substring(0, index).trim());
			setFirstName(pName.substring(index + 1).trim());
		}
	}
	public void setFirstName(String pFirstName) {
		super.setName(pFirstName);
	}
	public String getFirstName() {
		return super.getName();
	}
	public void setSurname(String pSurname) {
		mSurname = pSurname;
	}

	public String getSurname() {
		return mSurname;
	}

	public String toString() {
		return getName();
	}
	
	private String mSurname;
}
